﻿create PROCEDURE Sync.SP_CommitteeBankAccountDeposites_ApplyChanges
@RawValues XML 
AS 
BEGIN
	 
		DECLARE @CommitteeBankAccountDeposites_Temp TABLE 
		(
			CommitteeBankAccountDepositID UniqueIdentifier,
			[CommitteeBankAccountID] uniqueidentifier,
				[DepositDate] datetime,
				[OperationNumber] nvarchar(50),
				[Notes] nvarchar(MAX),
			IsDeleted BIT, 
			LastUpdatedDate DATETIME
		)
		
	   INSERT INTO @CommitteeBankAccountDeposites_Temp 
	   SELECT	Tbl.Col.value('CommitteeBankAccountDepositID[1]','uniqueidentifier') CommitteeBankAccountDepositID,
                Tbl.Col.value('CommitteeBankAccountID[1]', 'uniqueidentifier') [CommitteeBankAccountID],
Tbl.Col.value('DepositDate[1]', 'datetime') [DepositDate],
Tbl.Col.value('OperationNumber[1]', 'nvarchar(50)') [OperationNumber],
Tbl.Col.value('Notes[1]', 'nvarchar(MAX)') [Notes],
				Tbl.Col.value('IsDeleted[1]', 'bit') IsDeleted,
				Tbl.Col.value('LastUpdatedDate[1]','DateTime') LastUpdatedDate
	   FROM   @RawValues.nodes('/DocumentElement/CommitteeBankAccountDeposites') Tbl(Col)
----------------------------------------------------------------------------------------

-- Insert ------------------------------------------------------------------------------

	INSERT INTO [dbo].[CommitteeBankAccountDeposites] ([CommitteeBankAccountDepositID],[CommitteeBankAccountID],[DepositDate],[OperationNumber],[Notes])
	SELECT t.[CommitteeBankAccountDepositID],t.[CommitteeBankAccountID],t.[DepositDate],t.[OperationNumber],t.[Notes]
	FROM  @CommitteeBankAccountDeposites_Temp t
	WHERE t.IsDeleted = 0
    AND NOT EXISTS
    (
        SELECT 1 FROM sync.CommitteeBankAccountDeposites_Tracking s
        WHERE t.CommitteeBankAccountDepositID = s.CommitteeBankAccountDepositID
    )
	-- put conflicts here.
	
-- Update -------------------------------------------------------------------------------
     
	UPDATE [dbo].[CommitteeBankAccountDeposites]
	SET [CommitteeBankAccountDeposites].[CommitteeBankAccountID] = t.[CommitteeBankAccountID],[CommitteeBankAccountDeposites].[DepositDate] = t.[DepositDate],[CommitteeBankAccountDeposites].[OperationNumber] = t.[OperationNumber],[CommitteeBankAccountDeposites].[Notes] = t.[Notes] 
	FROM [dbo].[CommitteeBankAccountDeposites] s JOIN @CommitteeBankAccountDeposites_Temp t 
    ON t.CommitteeBankAccountDepositID = s.CommitteeBankAccountDepositID
	WHERE	t.IsDeleted = 0 
	AND		t.LastUpdatedDate IS NOT NULL
	AND		t.LastUpdatedDate >= 
				 isnull((SELECT TOP 1 LastUpdatedDate 
				  FROM sync.CommitteeBankAccountDeposites_Tracking r
				  WHERE t.CommitteeBankAccountDepositID = r.CommitteeBankAccountDepositID)
                  ,t.LastUpdatedDate)
	-----------------------------------------------------------------------------------------

-- Delete -------------------------------------------------------------------------------

	DELETE [dbo].[CommitteeBankAccountDeposites]
    FROM @CommitteeBankAccountDeposites_Temp t JOIN [dbo].[CommitteeBankAccountDeposites] s
    ON t.CommitteeBankAccountDepositID = s.CommitteeBankAccountDepositID
	WHERE t.IsDeleted = 1

-----------------------------------------------------------------------------------------
	
END











